function code=golomb(word,M)

% source: http://en.wikipedia.org/wiki/Golomb_coding
%Cambio de paradigma: calculo las cosas que hay que codificar (parte unaria
%y binaria) pero en lugar de codificarlas las devuelvo. En otra parte nos
%preocupamos bien por codificarlas y escribirlas a un archivo, posiblemente
%NO en matlab!
bits = ceil(log2(M));
cutoff = 2^bits - M;

% unary coding for quotient
q=floor(word/M);
%pt1 =dec2bin(2^(q+1)-2,q+1);

% truncated binary encoding for remainder
r = mod(word,M);
%pt2 = dec2bin(r+cutoff*(r>=cutoff));
%len_pt2=length(pt2);
%if((len_pt2-bits>0) && (r>=cutoff))
%  % use #bits
%  pt2 = dec2bin(r+cutoff*(r>=cutoff),bits); %This does not imply loss of bits?? Should be lossless!
%end
%if ((len_pt2-(bits-1)>0) && (r<cutoff))
%  % use bits-1
%  pt2 = dec2bin(r+cutoff*(r>=cutoff),bits-1); %This does not imply loss of bits??
%end
code=[q r];